﻿using System;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Eneta.Portal.Common;
using Eneta.Portal.Common.Dto;
using Eneta.Portal.Common.Repository;
using Microsoft.SharePoint;

namespace Eneta.Portal.Events.Controls
{
    public class EventListControl : UserControl
    {
        protected Repeater rpt;

        protected HtmlGenericControl pager;
        protected HtmlGenericControl pagerPrevContainer;
        protected HtmlGenericControl pagerNextContainer;
        protected HtmlAnchor pagerPrevLink;
        protected HtmlAnchor pagerNextLink;

        protected HtmlGenericControl pagerBottom;
        protected HtmlGenericControl pagerBottomPrevContainer;
        protected HtmlGenericControl pagerBottomNextContainer;
        protected HtmlAnchor pagerBottomPrevLink;
        protected HtmlAnchor pagerBottomNextLink;

        private EventRepository _repository;

        private EventRepository Repository
        {
            get
            {
                if (_repository == null)
                    _repository = new EventRepository(SPContext.Current.Web);
                return _repository;
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            int page;
            var pageSize = 10;
            int.TryParse(Request.QueryString["page"], out page);
            if (page < 1)
                page = 1;

            var events = Repository.GetApprovedEvents(page, 10);

            if (Math.Max(1, page - 1) == 1)
                pagerPrevLink.HRef = "/sundmused/Lehed/default.aspx";
            else
                pagerPrevLink.HRef = "/sundmused/Lehed/default.aspx?page=" + Math.Max(1, page - 1);
            pagerBottomPrevLink.HRef = pagerPrevLink.HRef;

            if (Math.Min(events.PageCount, page + 1) == 1)
                pagerNextLink.HRef = "/sundmused/Lehed/default.aspx";
            else
                pagerNextLink.HRef = "/sundmused/Lehed/default.aspx?page=" + Math.Min(events.PageCount, page + 1);
            pagerBottomNextLink.HRef = pagerNextLink.HRef;

            if (page <= 1)
                pagerPrevContainer.Visible = false;
            if (page >= events.PageCount)
                pagerNextContainer.Visible = false;

            pagerBottomPrevContainer.Visible = pagerPrevContainer.Visible;
            pagerBottomNextContainer.Visible = pagerNextContainer.Visible;

            rpt.DataSource = events.Results;
            rpt.DataBind();
        }

        protected string GetEndDate(object dtoObject)
        {
            var dto = dtoObject as EventDto;
            if (dto == null)
                return string.Empty;

            //if (dto.StartTime.Date == dto.EndTime.Date)
            //    return string.Empty;

            return "- " + dto.EndTime.ToShortTimeString();
        }

        protected object Iif(bool condition, object trueValue, object falseValue)
        {
            return (condition ? trueValue : falseValue);
        }

        protected string GetAttendeesCount(int eventId)
        {
            try
            {
                var list = SPContext.Current.Web.Lists["Registrations"];
                //var queryString = "<Where><Eq><FieldRef Name='Event' LookupId='True' /><Value Type='Lookup'>{0}</Value></Eq></Where>";                
                //queryString = string.Format(queryString, eventId);
                //Logger.LogInfo("Attendee count query: " + queryString, "GetAttendeesCount");

                //var query = new SPQuery();
                //query.Query = queryString;

                //var items = list.GetItems(query);
                var items = list.Items;
                if (items == null)
                    return "0";

                var i = 0;
                foreach (SPListItem item in items)
                {
                    var eventValue = new SPFieldLookupValue(item["Event"].ToString());
                    if (eventValue.LookupId == eventId)
                        i++;
                }

                return i.ToString();
            }
            catch (Exception ex)
            {
                Logger.LogInfo("Attendee count error","GetAttendeesCount");
                Logger.LogException(ex);
                return "0";
            }
        }
    }
}